package math;

/**
 * Created by fengliejv on 2017/12/22.
 */
public class RangeSumQuery2DImmutable304 {
    int[][] dp;

    public RangeSumQuery2DImmutable304(int[][] matrix) {
        dp = new int[matrix.length][matrix[0].length];
        dp[0][0] = matrix[0][0];
        for (int i = 1; i < matrix.length; i++) {
            dp[i][0] = dp[i - 1][0] + matrix[i][0];
        }
        for (int i = 1; i < matrix[0].length; i++) {
            dp[0][i] = dp[0][i - 1] + matrix[0][i];
        }

        for (int i = 1; i < matrix.length; i++) {
            for (int j = 1; j < matrix[0].length; j++) {
                dp[i][j] = matrix[i][j]+dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
            }
        }
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        int a = 0;
        int b = 0;
        int ab = 0;
        if (row1 > 0) {
            a = dp[row1 - 1][col2];
        }
        if (col1 > 0) {
            b = dp[row2][col1 - 1];
        }
        if (row1 > 0 && col1 > 0) {
            ab = dp[row1 - 1][col1 - 1];
        }
        return dp[row2][col2] - a - b + ab;
    }
}
